var data_point_type = null;
$(function () {
    $.when(getdatadictionary("progect_type")).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'api/bprojectapplication/list',
            datatype: "json",
            colModel: [
                {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
                {label: '项目申请书名称', name: 'pAName', index: 'p_a_name', width: 80},
                {
                    label: '适用项目类别', name: 'pAType', index: 'p_a_type', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        var names = "";

                        for (d in data_point_type) {
                            var ss = cellvalue.replace(/\"/g, "").replace("[", "").replace("]", "").split(",");

                            for ( var i = 0; i <ss.length; i++)
                            {
                                if (ss[i]==data_point_type[d].dKey) {
                                    names += data_point_type[d].dValue + ",";
                                }
                            }


                        }
                        if (names.length > 0) {
                            names = names.substring(0, names.length - 1);
                        }
                        return names;
                    }
                },
                {
                    label: '操作', name: '', width: 80, formatter: function (cellvalue, options, rowObject) {

                    return "<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\",1)' ><i class='fa  fa-align-justify'></i>&nbsp;其他费用模板</a>"
                             +"&nbsp;&nbsp;<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\",2)' ><i class='fa  fa-align-justify'></i>&nbsp;估算简表</a>"
                }
                }

            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList: [10, 30, 50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth: true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader: {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames: {
                page: "page",
                rows: "limit",
                order: "order"
            },
            gridComplete: function () {
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            }
        })
    );


});


function show(guid,i) {
    var title ="其他费用模板";
    var content = "bothercost.html?aguid=" + guid;
    if(i==2) {
        var title ="估算简表";
        var content = "bestimate.html?aguid=" + guid;
    }
    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['90%', '90%'],
        content: content

    });

}

function getdatadictionary(sss) {

    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        if (sss == "progect_type") {
            data_point_type = r.returnlist;
        }


    });
}
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bProjectApplication: {
            pAName: null,
            pAType: []
        },
        data_point_type: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },

        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bProjectApplication = {
                pAName: null,
                pAType: []
            };
            vm.data_point_type = data_point_type;
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.data_point_type = data_point_type;
            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bProjectApplication.guid == null ? "api/bprojectapplication/save" : "api/bprojectapplication/update";

            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bProjectApplication),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bprojectapplication/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bprojectapplication/info/" + guid, function (r) {
                vm.bProjectApplication = r.bProjectApplication;
                vm.bProjectApplication.pAType = r.bProjectApplication.pAType.replace(/\"/g, "").replace("[", "").replace("]", "").split(",");

            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
              page: page
            }).trigger("reloadGrid");
        }
    }

});
